package com.tone.ji_volunteer.util;

import cn.hutool.http.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tone.ji_volunteer.pojo.Code;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author tone(ang)
 * version 1.0
 */
@Data
@Component
@ConfigurationProperties(prefix = "wx.login")
public class WxLogin {
    private String appId;
    private String appSecret;
    private String url;
    @Autowired
    private Gson gson;
    public Map<String, Object> getSession(String code) {
        String strUrl = url +"?appid=" +appId +"&secret=" + appSecret +"&js_code="+code+"&grant_type=authorization_code";
        String res = HttpUtil.get(strUrl);
        // 将 res 转换为 Code 对象
        Map<String, Object> resultMap = gson.fromJson(res, new TypeToken<Map<String, Object>>() {
        }.getType());
        return resultMap;
    }
}
